def prim(graph):
    vertexes,edges=graph[0],graph[1]
    visited=[vertexes.pop(0)]
    result=[]
    while vertexes:
        candidates=[]
        for vertex in visited:
            candidates+=[ edge for edge in edges if vertex in edge ]
        candidate=min(candidates,key=lambda x:x[-1])
        result.append(candidate)
        vertex1,vertex2=candidate[0],candidate[1]
        if vertex1 in vertexes:
            vertexes.remove(vertex1)
        if vertex1 not in visited:
            visited.append(vertex1)
        if vertex2 in vertexes:
            vertexes.remove(vertex2)
        if vertex2 not in visited:
            visited.append(vertex2)
        for i in range(len(edges)-1,-1,-1):
            if edges[i][0] in visited and edges[i][1] in visited:
                edges.pop(i)
    return result

vertexes=['A','B','C','D','S','T']
edges=[
    ('A','B',6),('A','C',3),('A','S',7),
    ('B','C',4),('B','D',2),('B','T',5),
    ('C','D',3),('C','S',8),('D','T',2)
]
graph=(vertexes,edges)
print(prim(graph))
